import { getInfo, login, logout,getUserAuthority } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    // token
    token: getToken(),
    // 用户明
    name: '',
    // 头像
    avatar: '',
    // 初始加载状态
    init: false,
    // 菜单数据
    menuList: [],
    // 按钮数据
    buttonList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  RES_SET_TOKEN: (state) => {
    state.token = null
  },

  SET_NAME: (state, name) => {
    state.name = name
  },
  RES_SET_NAME: (state) => {
    state.name = null
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  RES_SET_AVATAR: (state) => {
    state.avatar = null
  },

  SET_SYSTEM_MENU: (state, data) => {
    state.init = true
    state.menuList = data.userMenuList
    state.buttonList = data.userButtonList
  },
  RES_SET_SYSTEM_MENU: (state) => {
    state.init = false
    state.menuList = null
    state.buttonList = null
  }
}

const actions = {
  // 登录请求
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        if (response.status === 200) {
          // 保存token
          commit('SET_TOKEN', response.data.tokenValue)
          setToken(response.data.tokenValue)
          resolve()
        } else {
          reject(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (response.status === 200) {
          const { userInfo } = data
          if (!userInfo) {
            return reject(response.msg + '，请重新登录')
          }
          // 设置登录成功后展示的欢迎提示
          if (userInfo.userName != null && userInfo.userName != '') {
            commit('SET_NAME', userInfo.userName)
          } else {
            commit('SET_NAME', userInfo.loginId)
          }
          // 设置头像
          if (userInfo.userPicture != null && userInfo.userPicture != '') {
            commit('SET_AVATAR', userInfo.userPicture)
          } else {
            commit('SET_AVATAR', '/static/img/1.jpeg')
          }
          resolve(userInfo)
        } else {
          return reject(response.msg + '，请重新登录')
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 退出登录
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        // 重置vuex的数据
        commit('RESET_STATE')
        commit('RES_SET_NAME')
        commit('RES_SET_SYSTEM_MENU')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取菜单及按钮数据
  getUserMenu({ commit }) {
    return new Promise((resolve, reject) => {
      // 发送请求
      getUserAuthority().then(res => {
        // 获取到数据，将菜单和按钮保存到vuex状态管理中
        commit('SET_SYSTEM_MENU', res.data)
        // 正常钩子
        resolve()
      }).catch(error => {
        // 异常钩子
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

